package application;

import java.sql.*;

import utils.config.Config;

public class MainRead {
	
	
	
	public static void main(String[] args) throws Exception {
		MainRead mr = new MainRead();
		mr.read();
	}
	
	
	private String OUT_DB;
	private double[][] AREA_BBOX;
	
	public MainRead() {
		Config config = new Config(this.getClass().getSimpleName()+".ser"); 
		OUT_DB = (String)config.get("OUT_DB");
		AREA_BBOX = (double[][])config.get("AREA_BBOX");
	}
	
	public void read() throws Exception {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
	    Connection c = DriverManager.getConnection(OUT_DB);
	    Statement s = c.createStatement();	
	    
	    double[][] bbox = AREA_BBOX;
	    double minlon = bbox[0][0];
	    double minlat = bbox[0][1];
	    double maxlon = bbox[1][0];
	    double maxlat = bbox[1][1];
	    
	    String points = "'MULTIPOINT("+minlon+" "+minlat+","+maxlon+" "+minlat+","+maxlon+" "+maxlat+","+minlon+" "+maxlat+")'";
	    String query = "SELECT idBusiness,name,address,city,state,category,AsText(coord) AS coord FROM Business WHERE (Intersects(coord,GeomFromText("+points+")));";
	    ResultSet rs = s.executeQuery(query);
	    
	    while(rs.next()) {
	    	int idBusiness = rs.getInt("idBusiness");
	    	String name = rs.getString("name");
	    	String address = rs.getString("address");
	    	String city = rs.getString("city");
	    	String state = rs.getString("state");
	    	String category = rs.getString("category");
	    	String coord = rs.getString("coord");
	    	
	    	System.out.println(idBusiness+": "+name+", "+address+", "+city+", "+category+", ("+coord+")");
	    }
	    
	    s.close();
	    c.close();
	    System.out.println("Done");
	}
}
